/**
* 基础菜单 用户管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="昵称：">
        <el-input size="small" v-model="formInline.nickName" placeholder="输入昵称"></el-input>
      </el-form-item>
      <el-form-item label="用户名">
        <el-input size="small" v-model="formInline.username" placeholder="输入用户名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" style="width: 100%" v-loading="loading" element-loading-text="拼命加载中">

      <el-table-column sortable prop="nickName" label="昵称" width="300">
      </el-table-column>
      <el-table-column sortable prop="username" label="用户名" width="300">
      </el-table-column>
      <el-table-column sortable prop="email" label="邮箱" width="300">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" @click="handleAuthority(scope.row)">设置角色</el-button>
          <el-button size="mini" type="danger" @click="deleteById(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="50%" :close-on-click-modal=false @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="用户名" prop="username">
          <el-input size="small" v-model="editForm.username" auto-complete="off" placeholder="请输入用户名">
          </el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" v-if="title == '添加'">
          <el-input size="small" type="password" v-model="editForm.password" auto-complete="off" placeholder="请输入用户名">
          </el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input size="small" v-model="editForm.email" auto-complete="off" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input size="small" v-model="editForm.nickName" auto-complete="off" placeholder="请输入昵称">
          </el-input>
        </el-form-item>
        <el-form-item label="备注信息" prop="note">
          <el-input size="small" v-model="editForm.note" auto-complete="off" placeholder="请输入备注信息"></el-input>
        </el-form-item>
        <el-form-item label="管理员" prop="remark">
         <el-switch
           v-model="editForm.adminiStrators"
           active-color="#13ce66"
           inactive-color="#ff4949"
           :active-value="2"
           :inactive-value="1">
         </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">
          保存
        </el-button>
      </div>
    </el-dialog>

    <!-- 角色列表 -->
    <el-dialog title="设置角色" :visible.sync="roleVisible" width="30%" @click="authorityCloseDialog">
      <el-table ref="authorityTable" :data="roleData" style="width: 100%" height="500"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="id" label="id" width="120">
        </el-table-column>
        <el-table-column prop="roleCode" label="角色编码" width="120">
        </el-table-column>
        <el-table-column prop="roleName" label="角色名称" show-overflow-tooltip>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="authorityCloseDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="roleSubmitForm()">
          保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    deptList,
    deptSave,
    deptDelete
  } from '../../api/userMG'
  import Pagination from '../../components/Pagination'
  export default {
    data() {
      return {
        loading: false, //是显示加载
        editFormVisible: false, //控制编辑页面显示与隐藏
        title: '添加',
        editForm: {
          username: '',
          password: '',
          email: '',
          nickName: '',
          note: '',
          adminiStrators:1,
        },
        // rules表单验证
        rules: {
          username: [{
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          }],
          password: [{
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }],
          email: [{
            required: true,
            message: '请输入邮箱',
            trigger: 'blur'
          }],
          nickName: [{
            required: true,
            message: '请输入昵称',
            trigger: 'blur'
          }]
        },
        //搜索参数
        formInline: {
          page: 1,
          rows: 10,
          nickName: '',
          username: '',
        },
        listData: [], //用户数据
        // 分页参数
        pageparm: {
          currentPage: 1,
          pageSize: 10,
          total: 10
        },
        //角色列表数据
        roleData: [],
        // 角色列表显隐
        roleVisible: false,
        //选中的数据
        multipleSelection: {},
        //暂存用户信息
        umsAdmin: {},
        roleId: '',
      }
    },
    // 注册组件
    components: {
      Pagination
    },
    /**
     * 数据发生改变
     */

    /**
     * 创建完毕
     */
    created() {
      this.getdata()
    },

    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
      // 获取用户列表
      getdata() {
        this.loading = true
        this.$http({
          url: "sys/umsAdmin/findUmsAdminsByParam",
          method: 'GET',
          params: this.formInline
        }).then(res => {
          if (res.code == 200) {
            this.loading = false
            this.listData = res.data.list
            console.log(111,this.listData)
            this.pageparm.currentPage = res.data.pageNum
            this.pageparm.pageSize = res.data.pageSize
            this.pageparm.total = res.data.total
          } else {
            this.loading = false
          }
        })

      },
      // 查询所有角色
      getRoleData() {
        var that = this;
        this.$http({
          url: "sys/umsRole/findUmsRolesByParam",
          method: 'GET',
        }).then(res => {
          if (res.code == 200) {
            this.roleData = res.data
            console.log('roleId',this.roleId)
            console.log('Roles',res)
            if (this.roleId == '') {
              return;
            }
            var index = this.roleData.findIndex(item => {
              return item.id == this.roleId
            })
            setTimeout(() => {
              if (index != -1) {
                this.toggleSelection(this.roleData[index])
              }
            }, 500)

          }
        })

      },
      // 分页插件事件
      callFather(parm) {
        this.formInline.page = parm.currentPage
        this.formInline.rows = parm.pageSize
        this.getdata()
      },
      // 搜索事件
      search() {
        this.formInline.page = 1
        this.getdata()
      },
      //显示编辑界面
      handleEdit(row) {
        if (row != undefined && row != 'undefined') {
          console.log(row.adminiStrators)
          this.title = '修改'
          this.editForm.id = row.id
          this.editForm.username = row.username
          this.editForm.email = row.email
          this.editForm.nickName = row.nickName
          this.editForm.note = row.note
          this.editForm.adminiStrators = row.adminiStrators == 2 ? row.adminiStrators: 1

        } else {
          this.title = '添加'
          this.editForm.id = ''
          this.editForm.password = ''
          this.editForm.username = ''
          this.editForm.email = ''
          this.editForm.nickName = ''
          this.editForm.note = ''
          this.editForm.adminiStrators = 1
        }
        this.editFormVisible = true
      },
      // 编辑、增加页面保存方法
      submitForm(editData) {
        console.log(this.editForm)
        this.$refs[editData].validate(valid => {
          if (valid) {
            var url = '';
            if (this.editForm.id != '') {
              url = "sys/umsAdmin/update";
            } else {
              url = "sys/umsAdmin/insert"
            }
            this.$http({
              url: url,
              method: 'POST',
              data: this.editForm
            }).then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.code == 200) {
                this.getdata()
              }
            }).catch(res =>{
              console.log(res)
              this.$message.error(res.message)
            })
          } else {
            return false
          }
        })
      },
      //角色列表点击提交
      roleSubmitForm() {
        var param = {
          roleId:  this.multipleSelection.id,
          umsAdminId: this.umsAdmin.id
        }
        this.$http({
          url: "sys/umsAdminRole/insert",
          method: 'POST',
          data: param
        }).then(res => {
          if (res.code == 200) {
            this.roleVisible = false
          }
        })
      },
      // 删除用户
      deleteById(row) {
        this.$confirm('确定要删除吗?', '信息', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(() => {
            this.$http({
              url: "/sys/umsAdmin/delById/" + row.id,
              method: 'GET',
            }).then(res => {
              if (res.code == 200) {
                this.$message.success(res.msg)
                this.getdata();
              } else {
                this.$message.error(res.msg)
              }
            })

          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      },
      //点击设置角色按钮
      handleAuthority(rows) {

        //查询用户关联的角色列表
        this.$http({
          url: "/sys/umsAdminRole/findUmsAdminRoleByParam",
          method: 'GET',
          params: {
            umsAdminId: rows.id
          }
        }).then(res => {
          var roleId = res.data ? res.data.roleId : ""
          this.roleId = roleId;
          this.getRoleData()
          this.roleVisible = true;
          this.umsAdmin = rows

        })
      },
      // 关闭编辑、增加弹出框
      closeDialog() {
        this.editFormVisible = false
      },
      // 关闭权限列表弹出框
      authorityCloseDialog() {
        this.roleVisible = false
      },
      toggleSelection(row) {
        console.log(1, row)

        this.$refs.authorityTable.toggleRowSelection(row);
      },
      handleSelectionChange(selection) {
        if (selection.length > 1) {
          let del_row = selection.shift()
          this.$refs.authorityTable.toggleRowSelection(del_row, false) //设置这一行取消选中
        }
        console.log(123,selection)
        this.multipleSelection = selection[0];
      },

    }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }

  .userRole {
    width: 100%;
  }
</style>
